package org.wmq.Cache;

import org.wmq.Mapper.impl.MySimpleMapMapperImpl;
import org.wmq.model.Entity.MySimpleMap;
import org.wmq.service.MySimpleMapService;
import org.wmq.service.impl.MySimpleMapServiceImpl;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class LocalCache {
    private static Map<Long, MySimpleMap> cache = new HashMap<>();
    
    static {
        MySimpleMapService mySimpleMapService = new MySimpleMapServiceImpl();
        String filePrefix = MySimpleMapMapperImpl.MySimpleMapFilePrefix;
        File prefixFile = new File(filePrefix);
        for (String filename : prefixFile.list()) {
            try {
                MySimpleMap mySimpleMap = mySimpleMapService.selectById(Long.parseLong(filename.split("\\.")[0]));
                cache.put(mySimpleMap.getId(),mySimpleMap);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public static Map<Long,MySimpleMap> getCache(){
        return cache;
    }
    public static void add(Long key,MySimpleMap value){
        cache.put(key,value);
    }
    
    public static MySimpleMap get(Long key){
        return cache.get(key);
    }
    
    public static void remove(Long key){
        cache.remove(key);
    }
}
